package org.lanqiao.oa.controller;

import org.lanqiao.oa.dto.PermissionDTO;
import org.lanqiao.oa.mapper.ResultInfo;
import org.lanqiao.oa.service.PermissionService;
import org.lanqiao.oa.utils.BeanCopierUtil;
import org.lanqiao.oa.utils.CheckMapper;
import org.lanqiao.oa.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/permissionController")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取获取所有权限
     * @return
     */
    @RequestMapping("/getPermissionList")
    @ResponseBody
    public List<PermissionVo> getPermissionList(){
        return permissionService.getPermission();
    }

    /**
     * 获取获取所有权限，并根据角色id设置属性
     * @return
     */
    @RequestMapping("/getPermissionListTick")
    @ResponseBody
    public Set<PermissionVo> getPermissionListTick(String id){
        List<PermissionVo> permissionListByRoleId = permissionService.getPermissionListByRoleId(id);
        permissionListByRoleId.forEach(t -> t.setChecked(true));
        List<PermissionVo> permission = permissionService.getPermission();

        HashSet<PermissionVo> objects = new HashSet<>();

        permissionListByRoleId.forEach(t -> objects.add(t));
        permission.forEach(t -> objects.add(t));
        return objects;
    }

    /**
     * 跳转页面
     * @param pagePath
     * @return
     */
    @RequestMapping("/forwordPage")
    public String forwordPage(String pagePath,ModelMap modelMap){
        return "forward:/WEB-INF/qx/"+pagePath;
    }

    /**
     * 获取权限跳转到页面
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/getPermission")
    public String getPermission(String id, ModelMap modelMap){
        PermissionDTO permissionDto = permissionService.selectById(id);
        modelMap.put("permission",permissionDto);
        return "forward:/WEB-INF/qx/permission/detail.jsp";
    }
    /**
     * 获取权限
     * @param id
     * @return
     */
    @RequestMapping("/getPermissionBuId")
    @ResponseBody
    public PermissionDTO getPermissionByID(String id){
        PermissionDTO permissionDto = permissionService.selectById(id);
        return permissionDto;
    }

    /**
     * 添加权限
     * @param permissionDto
     * @return
     */
    @RequestMapping("/addPermission")
    @ResponseBody
    public ResultInfo addPermission(PermissionDTO permissionDto){
        //验证code是否重复
        ResultInfo resultInfo = permissionService.ifAvailable(permissionDto.getCode());
        if(!resultInfo.getStatus()){
            return resultInfo;
        }
        //判断code是否合法
        ResultInfo<Map<String, String>> mapResultInfo = CheckMapper.check2(permissionDto);

        if(mapResultInfo.getStatus()){
            PermissionVo permissionVo = new PermissionVo();
            BeanCopierUtil.copy(permissionDto,permissionVo);
            return ResultInfo.getResultInfo(permissionService.addPermission(permissionDto),"添加成功",permissionVo);
        }else {
            mapResultInfo.setErrorMsg("数据有误");
            return mapResultInfo;
        }

    }

    /**
     * 修改权限
     * @param permissionDto
     * @return
     */
    @RequestMapping("/updatePermission")
    @ResponseBody
    public ResultInfo updatePermission(PermissionDTO permissionDto, ModelMap modelMap){
        //后台验证数据是否正确
        if(CheckMapper.check(permissionDto)){
            //验证code是否重复
            ResultInfo resultInfo = permissionService.ifAvailable(permissionDto.getCode());
            if(resultInfo.getStatus()){
                return ResultInfo.getResultInfo(permissionService.updatePermission(permissionDto));
            }else {
                return resultInfo;
            }
        }else {
            return ResultInfo.getResultInfo(false,"数据有误！！");
        }

    }

    /**
     * 删除权限
     * @return
     */
    @RequestMapping("/deletePermission")
    @ResponseBody
    public ResultInfo deletePermission(String id) {
        boolean b = permissionService.deletePermissionById(id);
        if(b){
            return ResultInfo.getResultInfo(b,"删除成功");
        }else {
            return ResultInfo.getResultInfo(b,"删除失败");
        }

    }

    /**
     * 判断code是否重复
     * @param code
     * @return
     */
    @RequestMapping("/checkCode")
    @ResponseBody
    public ResultInfo checkCode(String code){
        return permissionService.ifAvailable(code);
    }

    /**
     * 根据角色id查询权限
     * @return
     */
    @RequestMapping("/getPermissionListByRoleId")
    @ResponseBody
    public List<PermissionVo> getPermissionListByRoleId(String rid){
        return permissionService.getPermissionListByRoleId(rid);
    }



}
